package com.zhonghui.controller;

import com.zhonghui.common.core.result.FwResult;
import com.zhonghui.dto.PageDto;
import com.zhonghui.dto.ProductCreateDto;
import com.zhonghui.service.ProductService;
import com.zhonghui.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: FL
 * @create: 2024 2024/5/3 13:18
 */
@RestController
@Api(value = "商品控制层")
@Slf4j
@Validated
public class ProductController {
    @Autowired
    ProductService productService;

    /**
     * 获取banner
     */
    @ApiOperation("获取banner")
    @GetMapping("/banner")
    @Cacheable(cacheNames = "banner",key = "'banner'")
    public FwResult getBanner(){
        return productService.getBanner();
    }

    /**
     * 获取商品分类(所有)
     * @return
     */
    @ApiOperation("获取商品分类")
    @GetMapping("/category")
    @Cacheable(cacheNames = "category",key = "'category'")
    public FwResult<List<CategoryVo>> getGoodsCategory(){

        return productService.getCategory();
    }



    /**
     * 根据parentId获取商品分类
     * @return
     */
    @ApiOperation("根据parentId获取商品分类")
    @PostMapping("/subcategories/{categoryId}")
    @CachePut(value = "subcategories", key = "#categoryId")
    public FwResult getCategory(@PathVariable Long categoryId){
        return productService.getSubcategories(categoryId);
    }

    /**
     * 获取热门商品
     * @param pageDto
     * @return
     */
    @ApiOperation("获取热门商品")
    @PostMapping("/product/getHot")
    @CachePut(value = "hotProducts",key = "#pageDto.current")
    public FwResult getHot(@RequestBody PageDto pageDto){
        return productService.getHot(pageDto);
    }

    /**
     * 商品分页 ：获取所有商品
     * @param pageDto
     * @return
     */
    @ApiOperation("商品分页")
    @PostMapping("/product")
    @CachePut(value = "products",key = "#pageDto.current")
    public FwResult getProductPage(@RequestBody PageDto pageDto){
        return productService.getAllProduct(pageDto);
    }

    /**
     * 根据商品分类获取所有商品
     */
    @ApiOperation("根据商品分类获取所有商品")
    @PostMapping("/product/category/{categoryId}")
    public FwResult getProductPageByCategory(@PathVariable Long categoryId,@RequestBody PageDto pageDto){
        return productService.getAllProductByCategory(categoryId, pageDto);
    }

    /**
     * 根据商品父分类id获取所有商品
     */
    @ApiOperation("根据商品父分类id获取所有商品")
    @PostMapping("/product/category/parent/{parentCategoryId}")
    public FwResult getProductPageByParentCategory(@PathVariable Long parentCategoryId,@RequestBody PageDto pageDto){
        return productService.getAllProductByParentCategory(parentCategoryId, pageDto);
    }

    /**
     * 搜索商品
     */
    @ApiOperation("搜索商品")
    @PostMapping("/product/search")
    @CachePut(value = "products",key = "#pageDto.current")
    public FwResult getProductPageBySearch(@RequestParam("keyword") @Validated String keyword,@RequestBody PageDto pageDto){
        return productService.searchByKeyword(keyword,pageDto);
    }

    /**
     * 商品详情
     * @return
     */
    @ApiOperation("商品详情")
    @GetMapping("/product/{id}")
    @Cacheable(value = "product",key = "#id")
    public FwResult getProductDetail(@PathVariable Long id){
        return productService.getProduct(id);
    }

    /**
     * 添加商品
     */
    @ApiOperation("添加商品")
    @PostMapping("/product/add")
    public FwResult addProduct(@RequestBody @Validated ProductCreateDto productCreateDto){
        return  productService.addProduct(productCreateDto);
    }

    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @DeleteMapping("/product/{id}")
    @CacheEvict(cacheNames = "products",key = "#id")
    public FwResult deleteProduct(@PathVariable Long id){
        return productService.deleteProduct(id);
    }
}
